<?php
/**
 * Url.php
 * 
 * @category    Madoqua
 * @package     Module
 * @subpackage  Blog
 */

/**
 * Blog_Url
 * 
 * url modifying magic
 * 
 * @category    Madoqua
 * @package     Module
 * @subpackage  Blog
 */
class Blog_Url
{
    /**
     * generate a slug for a given string
     *
     * note that it requires mbstring and an input string in
     * unicode (utf-8) encoding. Also note that the resulting
     * string is encoded in unicode (again)
     *
     * @param string $string
     * @return string
     */
    public static function slugify($string) {
        $string = utf8_decode($string);
        //decode it
        $string = htmlentities($string);
        //to html entities
        $string = strtolower($string);
        //to lower case

        $doUmlauts = (bool) Madoqua_Application::getConfiguration()->rewriting->encodeumlauts;
        //encode umlauts?
        if ($doUmlauts) {
            $string = preg_replace("/&(.)(uml);/", "$1e", $string);
            //something with umlauts...
            //may be beneficial for SEO
        }

        $string = preg_replace("/&(.)(acute|cedil|circ|ring|tilde|uml);/", "$1", $string);
        //remove weirdos
        $string = preg_replace("/([^a-z0-9]+)/", "-", html_entity_decode($string));
        //remove funky characters
        $string = trim($string, "-");
        //trim the result (so it doesn't start/end with "-")

        return utf8_encode($string);
    }
}
